package cn.anc.aonicardv.module.ui.album;

import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.bean.PhotoBean;
import cn.anc.aonicardv.event.BigImageDataEvent;
import cn.anc.aonicardv.module.adpter.album.ImageBrowserPageAdapter;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.DialogUtils;
import cn.anc.aonicardv.widget.ImageBrowserViewPager;


public class BigImageActivity extends BaseActivity  {

    @BindView(R2.id.tv_exit)
    TextView tvExit;
    @BindView(R2.id.tv_title_name)
    TextView tvTitle;
    @BindView(R2.id.vp)
    ImageBrowserViewPager mVp;
    private ImageBrowserPageAdapter mAdapter;
    private List<PhotoBean> photoBeanList;
    private int currentPosition;
    private DialogUtils dialogUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_big_image);
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initVariable() {
        dialogUtils = new DialogUtils();
    }

    @Override
    public void setListener() {
    }

    @Override
    public void initData() {
     }

    @OnClick(R2.id.tv_exit)
    public void back() {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(BigImageDataEvent event) {
        photoBeanList = event.photoBeanList;
        currentPosition = getIntent().getIntExtra(Constant.IntentKeyParam.POSITION, 0);
        updateTitleAndName(currentPosition);
        mAdapter = new ImageBrowserPageAdapter(this, photoBeanList,false);
        if (null!=mVp){
        mVp.setAdapter(mAdapter);
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                updateTitleAndName(currentPosition);

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        mVp.setCurrentItem(currentPosition);
        }
    }

    private void updateTitleAndName(int position) {
        String fileCreateTime = photoBeanList.get(position).fileCreateTime;
        if (fileCreateTime != null) {
            String title = fileCreateTime;
            tvTitle.setText(title);
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    // viewpager auto play
    private static final long ANIM_VIEWPAGER_DELAY = 10000;

    private Handler h = new Handler();
    private Runnable animateViewPager = new Runnable() {
        public void run() {
            if (null!=mVp&&photoBeanList.size() > 0&&mVp.getCurrentItem()<photoBeanList.size()-1) {
                mVp.setCurrentItem((mVp.getCurrentItem() + 1)
                        % photoBeanList.size(), true);
                h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
            }
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        if (h != null) {
            h.removeCallbacks(animateViewPager);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (h != null) {
                h.removeCallbacks(animateViewPager);
            }
        }
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onResume() {
        super.onResume();
        h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
    }

}
